<template>
  <div class="signup-modal" v-if="modalStore.showSignupModal">
    <div class="modal-backdrop" @click="modalStore.closeSignupModal"></div>
    <div class="modal-container">
      <div class="signup-card">
        <div class="close-button-container">
          <button class="close-button" @click="modalStore.closeSignupModal">×</button>
        </div>
        
        <h1 class="signup-title">{{ $t('signup.title') }}</h1>
        
        <form @submit.prevent="handleSignup">
          
          <div class="form-group">
            <label for="email">{{ $t('signup.email') }}</label>
            <input 
              id="email" 
              type="email" 
              v-model="email" 
              :placeholder="$t('signup.emailPlaceholder')"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="username">{{ $t('signup.username') }}</label>
            <input 
              id="username" 
              type="text" 
              v-model="username" 
              :placeholder="$t('signup.usernamePlaceholder')"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="code">{{ $t('signup.verificationCode') }}</label>
            <div class="code-input-group">
              <input 
                id="code" 
                type="text" 
                v-model="code" 
                :placeholder="$t('signup.codePlaceholder')"
                required
              />
              <button 
                type="button" 
                class="send-code-button" 
                @click="sendVerificationCode"
                :disabled="codeSending || codeCountdown > 0"
              >
                {{ codeButtonText }}
              </button>
            </div>
          </div>
          
          <div class="form-group">
            <label for="inviteCode">邀请码</label>
            <input 
              id="inviteCode" 
              type="text" 
              v-model="inviteCode" 
              placeholder="请输入邀请码"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="signup-password">{{ $t('signup.password') }}</label>
            <div class="relative">
              <input 
                id="signup-password" 
                :type="showPassword ? 'text' : 'password'" 
                v-model="password"
                :placeholder="$t('signup.passwordPlaceholder')"
                class="w-full pr-10"
                required
              />
              <button 
                type="button" 
                class="absolute inset-y-0 right-0 flex items-center justify-center px-3 text-gray-400 hover:text-white"
                @click="showPassword = !showPassword"
                aria-label="切换密码可见性"
              >
                <svg 
                  xmlns="http://www.w3.org/2000/svg" 
                  width="20" 
                  height="20" 
                  viewBox="0 0 24 24" 
                  fill="none" 
                  stroke="currentColor" 
                  stroke-width="2" 
                  stroke-linecap="round" 
                  stroke-linejoin="round"
                >
                  <template v-if="showPassword">
                    <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                    <circle cx="12" cy="12" r="3"></circle>
                  </template>
                  <template v-else>
                    <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                    <circle cx="12" cy="12" r="3"></circle>
                    <line x1="3" y1="3" x2="21" y2="21"></line>
                  </template>
                </svg>
              </button>
            </div>
          </div>
          
          <button type="submit" class="signup-button" :disabled="isLoading">
            {{ isLoading ? $t('signup.creating') : $t('signup.createAccount') }}
          </button>
          
          <div class="login-link">
            {{ $t('signup.haveAccount') }} <a href="#" @click.prevent="modalStore.openLoginModal">{{ $t('login.title') }}</a>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import message from '@/utils/message';
import { useUserStore } from '@/stores/user';
import { useModalStore } from '@/stores/modal';
import { getEmailCode } from '@/api/user';

const { t } = useI18n();
const userStore = useUserStore();
const modalStore = useModalStore();

const email = ref('');
const username = ref('');
const password = ref('');
const code = ref('');
const inviteCode = ref('');
const showPassword = ref(false);
const isLoading = ref(false);
const errorMessage = ref('');
const codeSending = ref(false);
const codeCountdown = ref(0);

// 有效的邀请码列表
const validInviteCodes = ['DAVH6SH7', 'W102X7OD', '3WIY56BO', '1M23VMEN', 'L1R2TFC4', 'V73K2YH9', 'UT2GPDS8'];

const codeButtonText = computed(() => {
  return codeCountdown.value > 0 ? `${codeCountdown.value}s` : t('signup.sendCode');
});

const handleSignup = async () => {
  isLoading.value = true;
  errorMessage.value = '';
  
  if (!email.value || !username.value || !password.value || !code.value || !inviteCode.value) {
    message.error('请填写所有必填字段');
    isLoading.value = false;
    return;
  }
  
  // 验证邀请码
  if (!validInviteCodes.includes(inviteCode.value)) {
    message.error('邀请码无效');
    isLoading.value = false;
    return;
  }
  
  try {
    const response = await userStore.register({
      email: email.value,
      username: username.value,
      password: password.value,
      code: code.value,
      inviteCode: inviteCode.value
    });
    
    if (response && response.code === 0) {
      modalStore.closeSignupModal();
      email.value = '';
      username.value = '';
      password.value = '';
      code.value = '';
      inviteCode.value = '';
    } else {
      message.error(response?.msg || t('signup.errorMessage'));
    }
  } catch (error) {
    console.error('注册失败:', error);
    message.error(error.message || t('signup.errorMessage'));
  } finally {
    isLoading.value = false;
  }
};

const sendVerificationCode = async () => {
  if (!email.value) {
    message.error(t('signup.emailRequired'));
    return;
  }
  
  try {
    codeSending.value = true;
    
    const response = await getEmailCode({ 
      email: email.value,
      type: 'register'
    });
    
    if (response && response.code === 0) {
      message.success(t('signup.codeSent'));
      startCodeCountdown();
    } else {
      message.error(response?.msg || t('signup.sendCodeError'));
    }
  } catch (error) {
    console.error('发送验证码失败:', error);
    message.error(t('signup.sendCodeError'));
  } finally {
    codeSending.value = false;
  }
};

const startCodeCountdown = () => {
  codeCountdown.value = 60;
  const timer = setInterval(() => {
    codeCountdown.value--;
    if (codeCountdown.value <= 0) {
      clearInterval(timer);
    }
  }, 1000);
};
</script>

<style scoped>
.signup-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}

.modal-container {
  position: relative;
  z-index: 1001;
  width: 100%;
  max-width: 400px;
  padding: 0 15px;
  animation: modalFadeIn 0.3s ease-out;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.signup-card {
  background-color: rgba(24, 24, 27, 0.8);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
  width: 100%;
  position: relative;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.close-button-container {
  position: absolute;
  top: 10px;
  right: 10px;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #999;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

.signup-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  color: white;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 0.3rem;
  color: white;
}

input {
  width: 100%;
  padding: 0.6rem;
  font-size: 0.9rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  outline: none;
  transition: border-color 0.3s;
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

input:focus {
  border-color: #4A99E9;
  box-shadow: 0 0 0 2px rgba(74, 153, 233, 0.2);
}

.signup-button {
  width: 100%;
  padding: 0.6rem;
  font-size: 1rem;
  font-weight: 600;
  color: #000;
  background-color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 0.8rem;
  transition: all 0.3s;
}

.signup-button:hover {
  background-color: #f0f0f0;
  transform: translateY(-2px);
}

.signup-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.login-link {
  text-align: center;
  font-size: 0.8rem;
  color: #ccc;
}

.login-link a {
  color: white;
  text-decoration: none;
  font-weight: 600;
}

.login-link a:hover {
  text-decoration: underline;
}

.code-input-group {
  display: flex;
  gap: 0.5rem;
}

.send-code-button {
  background-color: #4A99E9;
  color: white;
  padding: 0 0.8rem;
  border-radius: 8px;
  font-size: 0.8rem;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.2s;
  min-width: 80px;
  border: none;
}

.send-code-button:hover:not(:disabled) {
  background-color: #3a89d9;
}

.send-code-button:disabled {
  background-color: #6c757d;
  cursor: not-allowed;
  opacity: 0.7;
}

.relative {
  position: relative;
}

.absolute {
  position: absolute;
}

.inset-y-0 {
  top: 0;
  bottom: 0;
}

.right-0 {
  right: 0;
}

.flex {
  display: flex;
}

.items-center {
  align-items: center;
}

.justify-center {
  justify-content: center;
}

.px-3 {
  padding-left: 0.75rem;
  padding-right: 0.75rem;
}

.text-gray-400 {
  color: #9ca3af;
}

.hover\:text-white:hover {
  color: white;
}

.w-full {
  width: 100%;
}

.pr-10 {
  padding-right: 2.5rem;
}
</style>